using System;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = OR.Lazarus.Library.CPP.Notation;

namespace OR.Lazarus.Library.CPP.Notation.Lazarus
{
	/// <summary>
	/// Describes a set of rules for applying notation to base Lazarus types in various contexts.
	/// </summary>
   public class NotationMapDetail : EntityBase
   {
		protected string mDescription;
	
		protected Derived.TypeCollection mTypes;
		protected Derived.NameCollection mDefaultNames;

		public NotationMapDetail()
		{
			mTypes = new Derived.TypeCollection();
			mTypes.ParentEntity = this;
			mDefaultNames = new Derived.NameCollection();
			mDefaultNames.ParentEntity = this;
	
		}
	
		/// <summary>
		/// 
		/// </summary>
		public string Description
		{
			get
			{
				return ( mDescription );
			}
			set
			{
				mDescription = value;
			}
		}

		public Derived.TypeCollection Types
		{
			get
			{
				return ( mTypes );
			}
			set
			{
				mTypes = value;
			}
		}

		public Derived.NameCollection DefaultNames
		{
			get
			{
				return ( mDefaultNames );
			}
			set
			{
				mDefaultNames = value;
			}
		}

		public override void LoadXml( XmlElement pSource )
		{
			// Scalar Values
			XmlElement vDescription = ( XmlElement )pSource.SelectSingleNode( "lzn:description", PersistenceConfig.Namespaces );
			
			if ( vDescription != null )
			{
				mDescription = vDescription.InnerText;
			}
			
			XmlElement vTypes = ( XmlElement )pSource.SelectSingleNode( "lzn:types", PersistenceConfig.Namespaces );
			
			if ( vTypes != null )
			{
				mTypes.LoadXml( vTypes );
			}
			XmlElement vDefaultNames = ( XmlElement )pSource.SelectSingleNode( "lzn:default-names", PersistenceConfig.Namespaces );
			
			if ( vDefaultNames != null )
			{
				mDefaultNames.LoadXml( vDefaultNames );
			}
		}
		
		public override XmlElement SaveXml( XmlDocument p_docContext )
		{
			XmlElement vNewElement	= p_docContext.CreateElement( "lzn", "notation-map", "http://schemas.icitadel.com/Lazarus/CPP/Notation.xsd" );

			// Scalar Values
			if ( !String.IsNullOrEmpty( mDescription ) )
			{
				XmlElement	vDescription = p_docContext.CreateElement( "lzn", "description", "http://schemas.icitadel.com/Lazarus/CPP/Notation.xsd" );
				vDescription.InnerText = mDescription;
				vNewElement.AppendChild( vDescription );
			}
					

			// Collections
			
			XmlElement	vCollection	= null;
			
			vCollection = mTypes.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}
			
			vCollection = mDefaultNames.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}

			return ( vNewElement );
		}

		public virtual object Clone()
		{
			Derived.NotationMapDetail v_oReturn = new Derived.NotationMapDetail();
			
			v_oReturn.Description = mDescription;
			
			// FK Values

			// Collections

			foreach ( Derived.TypeDetail vDetail in mTypes )
			{
				v_oReturn.Types.Add( vDetail.CloneTyped() );
			}

			foreach ( Derived.NameDetail vDetail in mDefaultNames )
			{
				v_oReturn.DefaultNames.Add( vDetail.CloneTyped() );
			}
			
			return ( v_oReturn );
		}

		public virtual Derived.NotationMapDetail CloneTyped()
		{
			return ( ( Derived.NotationMapDetail )this.Clone() );
		}
   }
}
   